package vn.com.bookstore.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import vn.com.bookstore.dao.AccountDao;
import vn.com.bookstore.domain.model.Account;
import vn.com.bookstore.domain.model.SearchResult;
import vn.com.bookstore.utils.DaoHelper;

@Repository
public class AccountDaoImpl extends DaoHelper<Account> implements AccountDao{

	@SuppressWarnings("unchecked")
	@Override
	public Account getUserByName(String username, String role) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM account AC left join account_type AT on AC.type_account_id = AT.id ");
		sql.append("WHERE AC.username = ? AND AT.name = ? AND AC.active = 1");
		Object[] parameters = new Object[]{username, role};
		SearchResult<Account> result = query(sql.toString(), parameters, new AccountRowMapper());
		if(result.getSearchResult().size() == 0) return null;
		return result.getSearchResult().get(0);
	}
	
	@SuppressWarnings("rawtypes")
	class AccountRowMapper implements RowMapper{
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Account account = new Account();
			account.setId(rs.getInt("id"));
			account.setUsername(rs.getString("username"));
			account.setPassword(rs.getString("password"));
			account.setTypeAccountId(rs.getInt("type_account_id"));
			account.setEmail(rs.getString("email"));
			account.setActive(rs.getBoolean("active"));
			return account;
		}
	}

	@Override
	public int insertNewAccount(Account account) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO account (username, password, type_account_id, email, active)");
		sql.append(" VALUES (?, ?, ?, ?, ?)");
		Object[] params = new Object[]{
			account.getUsername(), account.getPassword(), account.getTypeAccountId(), 
			account.getEmail(), account.getActive()	
		};
		return insert(sql.toString(), params);
	}

	@Override
	public void updateAccount(Account account) {
		StringBuilder sql = new StringBuilder();
		sql.append("UPDATE account set username = ?, password=? ");
		sql.append(" WHERE id = ?");
		Object[] params = new Object[]{
			account.getUsername(), account.getPassword(), account.getId()
		};
		insert(sql.toString(), params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Account getAccountById(int id) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM account WHERE id = ? AND active = 1");
		Object[] parameters = new Object[]{id};
		SearchResult<Account> result = query(sql.toString(), parameters, new AccountRowMapper());
		if(result.getSearchResult().size() == 0) return null;
		return result.getSearchResult().get(0);
	}

	@Override
	public void deleteAccount(int id) {
		StringBuilder sql = new StringBuilder();
		sql.append("DELETE FROM account WHERE id = ?");
		Object[] param = new Object[]{id};
		delete(sql.toString(), param);
	}

	@Override
	public boolean checkExistedUsername(String username) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT count(id) FROM account WHERE username = '"
				+ username + "'");
		int rowNum;
		try {
			rowNum = getJdbcTemplate().queryForInt(sql.toString());
		} catch (DataAccessException e) {
			return true;
		}
		if (rowNum > 0) {
			return true;
		}
		return false;
	}

}
